<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <header class="app-header">
      <div class="header-logo">
        <img src="@/assets/imgs/logo.png" alt="logo">
        <h1>选课管理系统</h1>
      </div>
      <div class="header-user">
        <el-dropdown>
          <div class="user-info">
            <el-avatar :src="data.user.avatar" />
            <span>{{ data.user.name }}</span>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="router.push('/person')">个人资料</el-dropdown-item>
              <el-dropdown-item @click="router.push('/password')">修改密码</el-dropdown-item>
              <el-dropdown-item divided @click="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </header>

    <div class="app-main">
      <!-- 侧边栏 -->
      <aside class="app-sidebar">
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          :collapse="isCollapse"
          background-color="var(--bg-sidebar)"
          text-color="var(--text-light)"
          active-text-color="var(--accent-color)"
          router
          @select="handleSelect"
        >
          <el-menu-item index="/home" route="/home">
            <el-icon><HomeFilled /></el-icon>
            <span>系统首页</span>
          </el-menu-item>
          <el-sub-menu index="2">
            <template #title>
              <el-icon><Promotion /></el-icon>
              <span>信息管理</span>
            </template>
            <el-menu-item index="/notice" route="/notice" v-if="data.user.role === 'ADMIN'">
              <el-icon><Bell /></el-icon>
              <span>公告信息</span>
            </el-menu-item>
            <!-- <el-menu-item index="/college"><el-icon>
                <OfficeBuilding />
              </el-icon><span>学院信息</span></el-menu-item>
            <el-menu-item index="/speciality"><el-icon>
                <School />
              </el-icon><span>专业信息</span></el-menu-item> -->
            <el-menu-item index="/course"><el-icon>
                <Reading />
              </el-icon><span>课程信息</span></el-menu-item>
            <el-menu-item index="/choice"><el-icon>
                <Reading />
              </el-icon><span>选课信息</span></el-menu-item>
          </el-sub-menu>
          <el-sub-menu index="3" v-if="data.user.role === 'ADMIN'">
            <template #title>
              <el-icon>
                <Avatar />
              </el-icon>
              <span>用户管理</span>
            </template>
            <el-menu-item index="/admin"><el-icon>
                <User />
              </el-icon><span>管理员信息</span></el-menu-item>
            <el-menu-item index="/teacher"><el-icon>
                <User />
              </el-icon><span>教师信息</span></el-menu-item>
            <el-menu-item index="/student"><el-icon>
                <User />
              </el-icon><span>学生信息</span></el-menu-item>
          </el-sub-menu>
          <el-menu-item v-if="data.user.role === 'ADMIN'" index="/person"><el-icon>
              <User />
            </el-icon><span>个人资料</span></el-menu-item>
          <el-menu-item v-if="data.user.role === 'TEACHER'" index="/tPerson"><el-icon>
              <User />
            </el-icon><span>个人资料</span></el-menu-item>
          <el-menu-item v-if="data.user.role === 'STUDENT'" index="/sPerson"><el-icon>
              <User />
            </el-icon><span>个人资料</span></el-menu-item>
          <el-menu-item index="/password">
            <el-icon>
              <Lock />
            </el-icon>
            <span>修改密码</span>
          </el-menu-item>
          <el-menu-item index="login" @click="logout">
            <el-icon>
              <SwitchButton />
            </el-icon>
            <span>退出系统</span>
          </el-menu-item>
        </el-menu>
      </aside>

      <!-- 主内容区 -->
      <main class="app-content">
        <router-view @updateUser="updateUser" />
      </main>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()

// 计算当前激活的菜单项
const activeMenu = computed(() => route.path)

const data = reactive({
  user: JSON.parse(localStorage.getItem('system-user') || '{}')
})

// 处理菜单选择
const handleSelect = (index) => {
  if (index === 'login') {
    logout()
  } else {
    router.push(index)
  }
}

// 退出登录
const logout = () => {
  ElMessage.success('退出成功')
  localStorage.removeItem('system-user')
  router.push('/login')
}

// 更新用户信息
const updateUser = () => {
  data.user = JSON.parse(localStorage.getItem('system-user') || '{}')
}

// 检查登录状态
if (!data.user?.id) {
  ElMessage.error('请登录！')
  router.push('/login')
}
</script>

<style scoped>
.app-container {
  min-height: 100vh;
  background-color: var(--bg-primary);
}

.app-header {
  height: 60px;
  background: var(--bg-secondary);
  box-shadow: var(--shadow-sm);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 var(--spacing-lg);
  position: fixed;
  width: 100%;
  top: 0;
  z-index: 1000;
}

.header-logo {
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
}

.header-logo img {
  height: 40px;
}

.header-logo h1 {
  font-size: 1.5rem;
  color: var(--primary-color);
  margin: 0;
}

.app-main {
  display: flex;
  margin-top: 60px;
  min-height: calc(100vh - 60px);
}

.app-sidebar {
  width: 240px;
  background: var(--bg-sidebar);
  transition: all 0.3s;
}

.app-content {
  flex: 1;
  padding: var(--spacing-lg);
  overflow-y: auto;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .app-sidebar {
    width: 64px;
  }
  
  .app-content {
    padding: var(--spacing-md);
  }
}
</style>